//
//  CTVerificationView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/20.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTVerificationView.h"
#import "CTDebugManager.h"
#import "CTHeader.h"
#import "CTAppLanguage.h"
#import "UIViewAdditions.h"
#import "CTLoginView_Def.h"
#import "CTDeviceFunction.h"

#define CT_VERIFICATION_TEXTFIELD_PHONE_TAG 1000
#define CT_VERIFICATION_TEXTFIELD_INPUT_TAG 1001

@interface CTVerificationView ()<UITextFieldDelegate>
{
    UILabel * timerLabel;
    UILabel *describeLabel;
    UITextField *phoneTextField;
    UITextField *inputTextField;
    UILabel *titleLabel;
    
    UILabel *stateBtnLabel;
    UILabel *prLabel;
}
@end


@implementation CTVerificationView
@synthesize delegate;


- (void)dealloc
{
    [stateBtnLabel removeFromSuperview];
    stateBtnLabel = nil;
    [prLabel removeFromSuperview];
    prLabel = nil;
    [timerLabel removeFromSuperview];
    timerLabel = nil;
    [describeLabel removeFromSuperview];
    describeLabel = nil;
    [phoneTextField removeFromSuperview];
    phoneTextField = nil;
    [inputTextField removeFromSuperview];
    inputTextField = nil;
    [titleLabel removeFromSuperview];
    titleLabel = nil;
    
    delegate = nil;
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTVerificationView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT);
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [self addGestureRecognizer:tap];
        
        [self createRegisterTitle];
        [self createVerificationView];
    }
    return self;
}
- (void)createRegisterTitle
{
    //标题View
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 252)];
    [self addSubview:titleView];
    
    //返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 20, 44, 44);
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE_CLICK] forState:UIControlStateHighlighted];
    [titleView addSubview:backButton];
    
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0.1199*CT_HEADER_SCREEN_HEIGHT , CT_HEADER_SCREEN_WIDTH, 24)];
    titleLabel.textColor = CT_HEADER_GREEN_COLOR;
    titleLabel.font = [UIFont systemFontOfSize:19.0f];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:titleLabel];
    DeviceTypeAdaptation deviceType = [CTDeviceFunction deviceTypeForResolutionAdaptation];
    if (deviceType == iPhone4_4s)
    {
        titleLabel.font = [UIFont fontWithName:nil size:21];
    }
    else if (deviceType == iPhone5_5s_5c)
    {
        titleLabel.font = [UIFont fontWithName:nil size:21];
    }
    else
    {
        titleLabel.font = [UIFont fontWithName:nil size:24];
    }
    
    describeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, titleLabel.bottom+15, CT_HEADER_SCREEN_WIDTH, 10)];
    describeLabel.text = [CTAppLanguage localizedStringForKey:@"verificationView_describeLabel_text" withValue:nil withTable:@"Language"];
    describeLabel.font = [UIFont fontWithName:nil size:LOGINVIEW_DESCRIBE_FONTSIZE];
    describeLabel.textColor = LOGINVIEW_DESCRIBE_COLOR;
    describeLabel.font = [UIFont systemFontOfSize:15.0f];
    describeLabel.textAlignment = NSTextAlignmentCenter;
//    [titleView addSubview:describeLabel]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ;
}
-(void)createVerificationView
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-235)/2,describeLabel.bottom+0.0944*CT_HEADER_SCREEN_HEIGHT , 235, 218)];
    [self addSubview:view];
    
    UILabel *stateLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, view.width, 12)];
    stateLabel.text = [CTAppLanguage localizedStringForKey:@"verificationView_stateLabel_text" withValue:nil withTable:@"Language"];
    stateLabel.textColor = CT_HEADER_GREEN_COLOR;
    stateLabel.font = [UIFont fontWithName:nil size:10];
    [view addSubview:stateLabel];

    stateBtnLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, stateLabel.origin.y-7, view.width, 18)];
    
    stateBtnLabel.text = [CTAppLanguage localizedStringForKey:@"verificationView_stateLabel_stateName" withValue:nil withTable:@"Language"];
//    [CTAppLanguage localizedStringForKey:@"verificationView_getVerificationCode" withValue:nil withTable:@"Language"];
    stateBtnLabel.textAlignment = NSTextAlignmentRight;
    stateBtnLabel.font = [UIFont fontWithName:nil size:18];
    stateBtnLabel.textColor = CT_HEADER_GREEN_COLOR;
    [view addSubview:stateBtnLabel];
    
    UIButton *stateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [stateButton addTarget:self action:@selector(stateButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    stateButton.frame = CGRectMake( stateBtnLabel.right-44,stateBtnLabel.origin.y ,44 ,44 );
    [stateButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [view addSubview:stateButton];

    //输入手机号
    
    prLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, stateLabel.bottom+49, 33, 12)];
    prLabel.text = @"86";
    prLabel.textColor = CT_HEADER_DEEPLINE_COLOR;
    prLabel.textAlignment = NSTextAlignmentCenter;
    prLabel.font = [UIFont fontWithName:nil size:12];
    [view addSubview:prLabel];
    UIImageView *rImageView = [[UIImageView alloc]initWithFrame:CGRectMake(prLabel.right, prLabel.origin.y , 0.5, 15)];
    rImageView.image = [UIImage imageNamed:@"signup_cutline_number@2x"];
    [view addSubview:rImageView];
    
    phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(rImageView.right, rImageView.origin.y, 140-33, 15)];
    phoneTextField.textAlignment = NSTextAlignmentCenter;
    phoneTextField.keyboardType = UIKeyboardTypePhonePad;
    phoneTextField.font = [UIFont fontWithName:nil size:15];
    phoneTextField.textColor = CT_HEADER_DEEPLINE_COLOR;
    phoneTextField.placeholder = [CTAppLanguage localizedStringForKey:@"verificationView_pnone_text" withValue:nil withTable:@"Language"];
    phoneTextField.tag = CT_VERIFICATION_TEXTFIELD_PHONE_TAG;
    phoneTextField.delegate = self;
    [view addSubview:phoneTextField];
    
    UIImageView *phoneLabel = [[UIImageView alloc]initWithFrame:CGRectMake(0, phoneTextField.bottom+7, view.width-85-10, 0.5)];
    phoneLabel.image = [UIImage imageNamed:@"login_line_otherway"];
    [view addSubview:phoneLabel];
    
    UIButton * verificationCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    verificationCodeBtn.frame = CGRectMake(phoneTextField.right+10, stateBtnLabel.bottom+31, 85, 39);
    [verificationCodeBtn addTarget:self action:@selector(sendVerificationCodeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [verificationCodeBtn setBackgroundImage:[UIImage imageNamed:@"signup_bg_icon_yanzhengma"] forState:UIControlStateNormal];
    [verificationCodeBtn setBackgroundImage:[UIImage imageNamed:@"signup_bg_icon_yanzhengma_bukedianji"] forState:UIControlStateHighlighted];
    [view addSubview:verificationCodeBtn];
    
    
    //输入验证码
    inputTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, verificationCodeBtn.bottom+30, view.width, 15)];
    inputTextField.placeholder = [CTAppLanguage localizedStringForKey:@"verificationView_verNumber_text" withValue:nil withTable:@"Language"];
    inputTextField.textColor = CT_HEADER_DEEPLINE_COLOR;
    inputTextField.textAlignment = NSTextAlignmentCenter;
    inputTextField.keyboardType = UIKeyboardTypePhonePad;
    inputTextField.font = [UIFont fontWithName:nil size:15];
    inputTextField.tag = CT_VERIFICATION_TEXTFIELD_INPUT_TAG;
    inputTextField.delegate = self;
    [view addSubview:inputTextField];
    
    UIImageView *inputLabel = [[UIImageView alloc]initWithFrame:CGRectMake(0, inputTextField.bottom+10, view.width, 0.5)];
    inputLabel.image = [UIImage imageNamed:@"login_line_otherway"];
    [view addSubview:inputLabel];
    //验证按钮
    UIButton * verifyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    DeviceTypeAdaptation deviceType = [CTDeviceFunction deviceTypeForResolutionAdaptation];
    if (deviceType == iPhone5_5s_5c)
    {
        verifyBtn.frame = CGRectMake(0, inputLabel.bottom+33, view.width, 38);
    }
    else if(deviceType == iPhone4_4s)
    {
        verifyBtn.frame = CGRectMake(0, inputLabel.bottom+33, view.width, 38);
    }
    else
    {
        verifyBtn.frame = CGRectMake(0, inputLabel.bottom+38, view.width, 38);
    }
//    [verifyBtn setBackgroundImage:[UIImage imageNamed:@"login_bg_login"] forState:UIControlStateNormal];
//    [verifyBtn setBackgroundImage:[UIImage imageNamed:@"login_bg_login_click"] forState:UIControlStateHighlighted];
    [verifyBtn setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateNormal];
    [verifyBtn setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateHighlighted];
    [verifyBtn setTitle:[CTAppLanguage localizedStringForKey:@"verificationView_verifyLabel_text" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
    [verifyBtn addTarget:self action:@selector(verifyBtn:) forControlEvents:UIControlEventTouchUpInside];
    verifyBtn.layer.cornerRadius = 3;
    verifyBtn.layer.masksToBounds =YES;
    CALayer *btnlayer = [verifyBtn layer];
    btnlayer.borderColor = [CT_HEADER_GREEN_COLOR CGColor];
    btnlayer.borderWidth = 0.5f;
    [view addSubview:verifyBtn];
    
    
    
    timerLabel = [[UILabel alloc] initWithFrame:CGRectMake(verificationCodeBtn.origin.x, verificationCodeBtn.origin.y, verificationCodeBtn.width, verificationCodeBtn.height)];
    timerLabel.backgroundColor = [UIColor clearColor];
    timerLabel.textColor = CT_HEADER_WHITE_COLOR;
    timerLabel.text = [CTAppLanguage localizedStringForKey:@"verificationView_getVerificationCode" withValue:nil withTable:@"Language"];
    timerLabel.font = [UIFont fontWithName:@"Arial" size:14];
    timerLabel.textAlignment = NSTextAlignmentCenter;
    [view addSubview:timerLabel];
    
    
    //用户协议
    
    UIView *proView = [[UIView alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-190)/2, CT_HEADER_SCREEN_HEIGHT-30, 190, 10)];
    [self addSubview:proView];
    NSString *proStr = [CTAppLanguage localizedStringForKey:@"thirdPartyLoginView_protocol_label_text" withValue:nil withTable:@"Language"];
    UILabel *protocolLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,proStr.length*10, 10)];
    protocolLabel.text = proStr;
    protocolLabel.textAlignment = NSTextAlignmentCenter;
    protocolLabel.textColor = CT_HEADER_GREEN_COLOR;
    protocolLabel.font = [UIFont fontWithName:nil size:10];
    [proView addSubview:protocolLabel];
    
    NSString *proLableStr = [CTAppLanguage localizedStringForKey:@"thirdPartyLoginView_protocolLabel_text" withValue:nil withTable:@"Language"];
    UILabel *protocolBtnLabel = [[UILabel alloc]initWithFrame:CGRectMake(protocolLabel.right+5, protocolLabel.origin.y, proLableStr.length*10, 10)];
    protocolBtnLabel.text = proLableStr;
    protocolBtnLabel.textAlignment = NSTextAlignmentLeft;
    protocolBtnLabel.textColor = CT_HEADER_GREEN_COLOR;
    protocolBtnLabel.font = [UIFont fontWithName:nil size:10];
    [proView addSubview:protocolBtnLabel];
    
    UIImageView *proImageView = [[UIImageView alloc]initWithFrame:CGRectMake(protocolBtnLabel.origin.x, protocolBtnLabel.bottom+2, protocolBtnLabel.width-40, 0.5)];
    proImageView.image = [UIImage imageNamed:@"login_line_username"];
    [proView addSubview:proImageView];
    
    
    UIButton *protocolBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    protocolBtn.frame = CGRectMake(protocolLabel.right+5, protocolLabel.origin.y, protocolBtnLabel.width, 20);
    [protocolBtn addTarget:self action:@selector(protoclBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [proView addSubview:protocolBtn];
}


#pragma mark send verification code
//用户协议
- (void)protoclBtnAction:(UIButton *) sender
{
    if([self.delegate respondsToSelector:@selector(verificationProBtnAction:)])
    {
        [self.delegate verificationProBtnAction:self];
    }
}
//返回按钮
- (void)backButtonAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(verificationViewBackButton:)])
    {
        [self.delegate verificationViewBackButton:self];
    }
}
//选择国家
-(void)stateButtonAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(verificationViewStateButton:)])
    {
        [self.delegate verificationViewStateButton:self];
    }
}
-(void)sendVerificationCodeBtn:(id)sender
{
    [phoneTextField resignFirstResponder];
    if ([self.delegate respondsToSelector:@selector(verificationCodeAction:)]) {
        [self.delegate verificationCodeAction:self];
    }
}


-(void)setText:(NSString *)timerText
{
    timerLabel.text = timerText;
}

-(void)setTitleView:(CTVerificationPage)pageType
{
    if (pageType == CTVerificationPageRegister)
    {
        titleLabel.text = [CTAppLanguage localizedStringForKey:@"verificationView_titleLabel_text" withValue:nil withTable:@"Language"];
        describeLabel.text = [CTAppLanguage localizedStringForKey:@"verificationView_describeLabel_text" withValue:nil withTable:@"Language"];
    }
    else if (CTVerificationPageResetPassword)
    {
        titleLabel.text = [CTAppLanguage localizedStringForKey:@"verificationView_title_text" withValue:nil withTable:@"Language"];
        describeLabel.text = [CTAppLanguage localizedStringForKey:@"verificationView_des_text" withValue:nil withTable:@"Language"];
    }
    else
    {
            
    }
        
}
-(void)setAreaName:(NSString *)areaName
{
    stateBtnLabel.text = areaName;
}

-(void)setAreaCode:(NSString *)areaCode
{
    prLabel.text = areaCode;
}

#pragma mark verify 

-(void)verifyBtn:(id)sender
{
    [phoneTextField resignFirstResponder];
    [inputTextField resignFirstResponder];
    if ([self.delegate respondsToSelector:@selector(verifyAction:)]) {
        [self.delegate verifyAction:self];
    }
}

#pragma mark TextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    switch (textField.tag) {
        case CT_VERIFICATION_TEXTFIELD_PHONE_TAG:
            if ([self.delegate respondsToSelector:@selector(verificationViewPhoneTextFieldText:registerView:)])
            {
                [self.delegate verificationViewPhoneTextFieldText:textField.text registerView:self];
            }
            break;
        case CT_VERIFICATION_TEXTFIELD_INPUT_TAG:
            if ([self.delegate respondsToSelector:@selector(verificationViewPhoneTextFieldText:registerView:)])
            {
                [self.delegate verificationViewInputTextFieldText:textField.text registerView:self];
            }
            break;
        default:
            break;
    }

}

//收回键盘
- (void)tapAction:(UITapGestureRecognizer *) tap
{
    [phoneTextField resignFirstResponder];
    [inputTextField resignFirstResponder];
}
@end
